﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;
using System.Xml.Serialization;

namespace Client.model
{
    public class UserManager : INotifyPropertyChanged
    {
        string fileName = "userData.xml";
        ObjectCodec objectCodec;
        User user = new User();
        ObservableCollection<User> users = new ObservableCollection<User>();
        ObservableCollection<User> usersBackup = new ObservableCollection<User>();
      
        public UserManager() {
            MakeSampleUser();
            Initialize();
        }

        public void Initialize() {
            if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                users.Add(new User() { Id = 1, Name = "Park", Tel = "119" });
                users.Add(new User() { Id = 2, Name = "Han", Tel = "911" });
            }
            else
            {
                objectCodec = new ObjectCodec();
                users = objectCodec.DeSerializeObject<ObservableCollection<User>>(fileName);
                if (users != null)
                    foreach (User oneUser in users)
                    {
                        User user = new User();
                        user.copyData(oneUser);
                        usersBackup.Add(user);
                    }
                if (users == null) users = new ObservableCollection<User>();
            }
        }

        private void MakeSampleUser()
        {
            user.Name = "kim";
            user.Address = "서울";
            user.Age = 50;
            user.Tel = "01024935817";
            user.Exp = false;
            user.Sex = true;
            user.Note = "테스트";
        }

        internal void UserSelected(object selectedItem)
        {
            User = selectedItem as User;
        }

        internal void UserAdd()
        {
            User newUser = new User();
            newUser.copyData(user);
            newUser.Id = users.Count;
            usersBackup.Add(newUser);

            users.Clear();
            foreach (User oneUser in usersBackup)
            {
                User user1 = new User();
                user1.copyData(oneUser);
                users.Add(user1);
            }
            SaveUser();            
        }

        internal void SaveUser()
        {
            objectCodec.SerializeObject(Users, fileName);
            usersBackup.Clear();
            foreach (User oneUser in users)
            {
                User user = new User();
                user.copyData(oneUser);
                usersBackup.Add(user);
            }
        }

        internal void DeleteUser()
        {
            for (int i = 0; i < users.Count; i++) {
                if (users[i].Id == user.Id) users.RemoveAt(i);
            }
        }

        public User User
        {
            get { return user; }
            set { user = value; NotifyPropertyChanged("User"); }
        }

        public ObservableCollection<User> Users
        {
            get { return users; }
            set { users = value; }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        
    }
}
